import Vue from "vue";
import Router from "vue-router";
import store from "@/store.js";
import { message } from "ant-design-vue";
import commonJs from "@/lib/common.js";
commonJs.isPC();
Vue.use(Router);
let [index] = [];
let [company] = [];
let [companyInfo] = [];
let [person] = [];
let [withdraw, recharge] = [];
if (store.state.isPC) {
  index = () => import("./views/index/index.vue");
  company = () => import("./views/company/index.vue");
  person = () => import("./views/person/index.vue");
  companyInfo = () => import("./views/companyInfo/index.vue");
  withdraw = () => import("./views/withdraw/index.vue");
  recharge = () => import("./views/recharge/index.vue");
} else {
  index = () => import("./viewsMobile/index/mobile.vue");
  company = () => import("./viewsMobile/company/mobile.vue");
  person = () => import("./viewsMobile/person/mobile.vue");
  companyInfo = () => import("./viewsMobile/companyInfo/mobile.vue");
  withdraw = () => import("./viewsMobile/withdraw/index.vue");
  recharge = () => import("./viewsMobile/recharge/index.vue");
}
const home = () => import("./viewsMobile/home/index.vue");
const login = () => import("./views/login/index.vue");
const authentication = () => import("./views/authentication/index.vue");
const loginPassword = () => import("./views/person/changePsw.vue");
const companyInfoForm = () => import("./views/companyInfo/companyInfoForm.vue");
const payPassword = () => import("./views/person/changePsw.vue");
const bankCard = () => import("./views/person/bankCard.vue");
const addMoney = () => import("./views/money/add.vue");
const addProject = () => import("./views/projectList/add.vue");
const editProject = () => import("./views/projectList/add.vue");
const detailProject = () => import("./views/projectList/detail.vue");
const back = () => import("./views/back/index.vue");
const backMoney = () => import("./views/back/back.vue");
const payBack = () => import("./views/back/payBack.vue");
const rechargePage = () => import("./views/recharge/recharge.vue");
const withdrawPage = () => import("./views/withdraw/withdraw.vue");
const messages = () => import("./views/message/index.vue");
const money = () => import("./views/money/index.vue");
const projectList = () => import("./views/projectList/index.vue");
let router = new Router({
  // mode: "history",
  base: process.env.BASE_URL,
  routes: [
    {
      path: "/",
      redirect: "/login"
    },
    {
      path: "/login",
      name: "login",
      component: login,
      meta: { name: "登录", activeNav: "companyInfo", isShowNav: true }
    },
    {
      path: "/authentication",
      name: "authentication",
      component: authentication,
      meta: {
        name: "认证",
        ath: true, // 是否需要登录
        athMobile: false,
        isShowNav: true,
        activeNav: "companyInfo"
      }
    },
    {
      path: "/index",
      name: "index",
      component: index,
      meta: {
        name: "首页",
        ath: false, // 是否需要登录
        athMobile: false,
        isShowNav: true,
        activeNav: "companyInfo"
      },
      children: [
        {
          path: "company",
          name: "company",
          component: company,
          meta: {
            name: "首页",
            ath: true, // 是否需要登录
            athMobile: false,
            isShowNav: true
          },
          children: [
            {
              path: "home",
              name: "home",
              component: home,
              meta: {
                name: "首页",
                isShowNav: true,
                ath: true, // 是否需要登录
                athMobile: false, // 是否需要手机认证
                activeNav: "home"
              }
            },
            {
              path: "person",
              name: "person",
              component: person,
              meta: {
                name: "安全设置",
                isShowNav: true,
                ath: true, // 是否需要登录
                athMobile: true, // 是否需要手机认证
                athStatus: true, // 是否需要成功审核后使用
                activeNav: "person"
              }
            },
            {
              path: "message",
              name: "message",
              component: messages,
              meta: {
                name: "消息通知",
                isShowNav: true,
                ath: true, // 是否需要登录
                athMobile: true, // 是否需要手机认证
                athStatus: true, // 是否需要成功审核后使用
                activeNav: "message"
              }
            },
            {
              path: "company-info",
              name: "companyInfo",
              component: companyInfo,
              meta: {
                name: "公司资料",
                isShowNav: false,
                ath: true, // 是否需要登录
                athMobile: false, // 是否需要手机认证
                activeNav: "companyInfo"
              }
            },
            {
              path: "company-info-form",
              name: "companyInfoForm",
              component: companyInfoForm,
              meta: {
                name: "完善资料",
                isShowNav: false,
                ath: true, // 是否需要登录
                athMobile: false, // 是否需要手机认证
                activeNav: "companyInfo"
              }
            },
            {
              path: "login-password",
              name: "loginPassword",
              component: loginPassword,
              meta: {
                name: "登录密码",
                isShowNav: false,
                ath: true, // 是否需要登录
                athMobile: true, // 是否需要手机认证
                athStatus: true, // 是否需要成功审核后使用
                activeNav: "person"
              }
            },
            {
              path: "pay-password",
              name: "payPassword",
              component: payPassword,
              meta: {
                name: "支付密码",
                isShowNav: false,
                ath: true, // 是否需要登录
                athMobile: true, // 是否需要手机认证
                athStatus: true, // 是否需要成功审核后使用
                activeNav: "person"
              }
            },
            {
              path: "bank-card",
              name: "bankCard",
              component: bankCard,
              meta: {
                name: "银行卡管理",
                isShowNav: false,
                ath: true, // 是否需要登录
                athMobile: true, // 是否需要手机认证
                athStatus: true, // 是否需要成功审核后使用
                activeNav: "person"
              }
            },
            {
              path: "project-list",
              name: "projectList",
              component: projectList,
              meta: {
                name: "项目管理",
                isShowNav: false,
                ath: true, // 是否需要登录
                athMobile: true, // 是否需要手机认证
                athStatus: true, // 是否需要成功审核后使用
                activeNav: "projectList"
              }
            },
            {
              path: "add-project",
              name: "addProject",
              component: addProject,
              meta: {
                name: "新增项目",
                isShowNav: false,
                ath: true, // 是否需要登录
                athMobile: true, // 是否需要手机认证
                athStatus: true, // 是否需要成功审核后使用
                activeNav: "projectList"
              }
            },
            {
              path: "edit-project/:id",
              name: "editProject",
              component: editProject,
              meta: {
                name: "编辑项目",
                isShowNav: false,
                ath: true, // 是否需要登录
                athMobile: true, // 是否需要手机认证
                athStatus: true, // 是否需要成功审核后使用
                activeNav: "projectList"
              }
            },
            {
              path: "detail-project/:id",
              name: "detailProject",
              component: detailProject,
              meta: {
                name: "项目详情",
                isShowNav: false,
                ath: true, // 是否需要登录
                athMobile: true, // 是否需要手机认证
                athStatus: true, // 是否需要成功审核后使用
                activeNav: "projectList"
              }
            },
            {
              path: "money",
              name: "money",
              component: money,
              meta: {
                name: "用款申请",
                isShowNav: false,
                ath: true, // 是否需要登录
                athMobile: true, // 是否需要手机认证
                athStatus: true, // 是否需要成功审核后使用
                activeNav: "money"
              }
            },
            {
              path: "add-money",
              name: "addMoney",
              component: addMoney,
              meta: {
                name: "发起用款申请",
                isShowNav: false,
                ath: true, // 是否需要登录
                athMobile: true, // 是否需要手机认证
                athStatus: true, // 是否需要成功审核后使用
                activeNav: "money"
              }
            },
            {
              path: "withdraw-index",
              name: "withdraw",
              component: withdraw,
              meta: {
                name: "提现管理",
                isShowNav: false,
                ath: true, // 是否需要登录
                athMobile: true, // 是否需要手机认证
                athStatus: true, // 是否需要成功审核后使用
                activeNav: "withdraw"
              }
            },
            {
              path: "recharge-index",
              name: "recharge",
              component: recharge,
              meta: {
                name: "充值管理",
                isShowNav: false,
                ath: true, // 是否需要登录
                athMobile: true, // 是否需要手机认证
                athStatus: true, // 是否需要成功审核后使用
                activeNav: "recharge"
              }
            },
            {
              path: "withdraw",
              name: "withdrawPage",
              component: withdrawPage,
              meta: {
                name: "提现",
                isShowNav: false,
                ath: true, // 是否需要登录
                athMobile: true, // 是否需要手机认证
                athStatus: true, // 是否需要成功审核后使用
                activeNav: "withdraw"
              }
            },
            {
              path: "recharge",
              name: "rechargePage",
              component: rechargePage,
              meta: {
                name: "充值",
                isShowNav: false,
                ath: true, // 是否需要登录
                athMobile: true, // 是否需要手机认证
                athStatus: true, // 是否需要成功审核后使用
                activeNav: "recharge"
              }
            },
            {
              path: "back",
              name: "back",
              component: back,
              meta: {
                name: "回款管理",
                isShowNav: false,
                ath: true, // 是否需要登录
                athMobile: true, // 是否需要手机认证
                athStatus: true, // 是否需要成功审核后使用
                activeNav: "back"
              }
            },
            {
              path: "back-money/:id",
              name: "backMoney",
              component: backMoney,
              meta: {
                name: "回款",
                isShowNav: false,
                ath: true, // 是否需要登录
                athMobile: true, // 是否需要手机认证
                athStatus: true, // 是否需要成功审核后使用
                activeNav: "back"
              }
            },
            {
              path: "pay-back/:id",
              name: "payBack",
              component: payBack,
              meta: {
                name: "回款支付",
                isShowNav: false,
                ath: true, // 是否需要登录
                athMobile: true, // 是否需要手机认证
                athStatus: true, // 是否需要成功审核后使用
                activeNav: "back"
              }
            }
          ]
        }
      ]
    },
    {
      path: "*",
      name: "notFound",
      component: () => import("./views/notFound/index.vue"),
      meta: {
        name: "404",
        activeNav: "companyInfo",
        isShowNav: false
      }
    }
  ]
});
export default router;
router.beforeEach(function(to, from, next) {
  if (from.matched.length === 0) {
    let state = sessionStorage.getItem("state");
    if (state) {
      store.replaceState(JSON.parse(state));
    }
  }
  store.dispatch("changeIsShowNav", to.meta.isShowNav);
  store.dispatch("changeTitle", to.meta.name);
  store.dispatch("changeNavRouterWeb", to.meta.activeNav);
  if (to.meta.ath) {
    let token = localStorage.getItem("ctoken");
    if (!token) {
      router.replace("/");
    }
    if (from.matched.length === 0) {
      commonJs.getCompanyInfo(() => {
        if (to.meta.athMobile) {
          if (!store.state.validateMobile) {
            router.replace("/authentication");
          }
        }
        if (to.meta.athStatus) {
          if (store.state.checkStatus !== "5") {
            message.warning("您的企业信息尚未审核成功，待审核成功开启该权限");
            router.replace("index/company/home");
          }
        }
      });
    } else {
      if (to.meta.athMobile) {
        if (!store.state.validateMobile) {
          router.replace("/authentication");
        }
      }
      if (to.meta.athStatus) {
        if (store.state.checkStatus !== "5") {
          message.warning("您的企业信息尚未审核成功，待审核成功开启该权限");
          router.replace("index/company/home");
        }
      }
    }
  }
  next();
});
